home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.2 / Bullet / sofdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  12.1 KB  |  412 lines

  1. /*
  2. **    $Id$
  3. **
  4. **    DevCon/ScalableOutlineFonts/sofdemo.c
  5. **
  6. **    (C) Copyright 1991 Robert R. Burns
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/libraries.h>
  12. #include <dos/dos.h>
  13. #include <graphics/rastport.h>
  14. #include <graphics/text.h>
  15. #include <intuition/intuition.h>
  16. #include "libraries/diskfont.h"
  17. #include "libraries/diskfonttag.h"
  18. #include <utility/tagitem.h>
  19.  
  20. #include <clib/exec_protos.h>
  21. #include <clib/diskfont_protos.h>
  22. #include <clib/dos_protos.h>
  23. #include <clib/graphics_protos.h>
  24. #include <clib/intuition_protos.h>
  25. #include <clib/utility_protos.h>
  26. #include <pragmas/exec_pragmas.h>
  27. #include <pragmas/diskfont_pragmas.h>
  28. #include <pragmas/dos_pragmas.h>
  29. #include <pragmas/graphics_pragmas.h>
  30. #include <pragmas/intuition_pragmas.h>
  31. #include <pragmas/utility_pragmas.h>
  32.  
  33. extern struct Library near *SysBase;
  34. extern struct Library near *DOSBase;
  35. extern int (*_ONBREAK)();
  36.  
  37. struct Library near *DiskfontBase = (struct Library *) 0;
  38. struct GfxBase near *GfxBase = (struct Libary *) 0;
  39. struct Library near *IntuitionBase = (struct Library *) 0;
  40. struct Library near *UtilityBase = (struct Library *) 0;
  41.  
  42. struct Screen *LockedScreen = 0;
  43. struct Screen *Screen = 0;
  44.  
  45. struct AvailFontsHeader *AFH = 0;
  46. struct TAvailFonts *TAF;
  47.  
  48. char FilePath[256];
  49. char FaceName[32];
  50.  
  51. struct NewWindow NW = {         /* 1.3 compatable part of window definition */
  52.     0, 0,            /* LeftEdge, TopEdge */
  53.     640, 0,            /* Width, Height (filled in later) */
  54.     -1, -1,            /* DetailPen, BlockPen */
  55.     CLOSEWINDOW|NOCAREREFRESH,    /* IDCMPFlags */
  56.     SMART_REFRESH|        /* Flags */
  57.     WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG,
  58.     0, 0,            /* FirstGadget, CheckMark */
  59.     0,                /* Title */
  60.     0, 0,            /* Screen, BitMap */
  61.     1, 1,            /* MinWidth, MinHeight */
  62.     0, 0,            /* MaxWidth, MaxHeight */
  63.                 /*   (same as initial dimension) */
  64.     WBENCHSCREEN        /* Type */
  65. };
  66.  
  67. struct Window *Window = 0;
  68. struct RastPort *RastPort = 0;
  69.  
  70.  
  71. void
  72. EndGame(code, format, args)
  73. int code;
  74. char *format, *args;
  75. {
  76.     struct EasyStruct ez;
  77.     struct Message *im;
  78.  
  79.     if (IntuitionBase && code) {
  80.     ez.es_StructSize = sizeof(struct EasyStruct);
  81.     ez.es_Flags = 0;
  82.     ez.es_Title = "Abnormal Termination";
  83.     ez.es_TextFormat = format;
  84.     ez.es_GadgetFormat = "OK";
  85.     EasyRequestArgs(Window, &ez, 0, &args);
  86.     }
  87.  
  88.     if (AFH)
  89.     FreeVec(AFH);
  90.     if (Window) {
  91.     while (im = GetMsg(Window->UserPort))
  92.         ReplyMsg(im);
  93.     CloseWindow(Window);
  94.     }
  95.     if (LockedScreen)
  96.     UnlockPubScreen(0, LockedScreen);
  97.     if (UtilityBase)
  98.     CloseLibrary(UtilityBase);
  99.     if (DiskfontBase)
  100.     CloseLibrary(DiskfontBase);
  101.     if (IntuitionBase)
  102.     CloseLibrary(IntuitionBase);
  103.     if (GfxBase)
  104.     CloseLibrary(GfxBase);
  105.  
  106.     exit(code);
  107. }
  108.  
  109.  
  110. void CXBRK()
  111. {
  112.     EndGame(1, "Control C Break\n");
  113. }
  114.  
  115.  
  116. void
  117. GetAvailFonts()
  118. {
  119.     int afShortage, afSize;
  120.  
  121.     afSize = 400;
  122.     do {
  123.     AFH = (struct AvailFontsHeader *) AllocVec(afSize, 0);
  124.     if (AFH) {
  125.         afShortage = AvailFonts((STRPTR) AFH, afSize,
  126.             AFF_MEMORY|AFF_DISK|AFF_TAGGED);
  127.         if (afShortage) {
  128.         FreeVec(AFH);
  129.         afSize += afShortage;
  130.         }
  131.     }    
  132.     else
  133.         EndGame(RETURN_FAIL, "AFH AllocVec failed\n");
  134.     }
  135.     while (afShortage);
  136.     TAF = (struct TAvailFonts *) (AFH+1);
  137. }
  138.  
  139.  
  140. struct TextFont *
  141. OpenAspectedFont(faceName, ySize, xPercent, style)
  142. char *faceName;
  143. UWORD ySize, xPercent;
  144. UBYTE style;
  145. {
  146.     struct TTextAttr tTextAttr;
  147.     struct TagItem tag, *tags, tagList[2];
  148.     struct FontContentsHeader fch;
  149.     BPTR fFile;
  150.     char *s;
  151.     int actual, i, x, y;
  152.  
  153.     strcpy(FaceName, faceName);
  154.     tTextAttr.tta_Name = FaceName;
  155.     tTextAttr.tta_YSize = ySize;
  156.     tTextAttr.tta_Style = style | FSF_TAGGED;
  157.     tTextAttr.tta_Flags = 0;
  158.     tTextAttr.tta_Tags = tagList;
  159.     tagList[0].ti_Tag = TA_DeviceDPI;
  160.     tagList[0].ti_Data = (100<<16) | xPercent;    /* xPercent -> YDPI */
  161.     tagList[1].ti_Tag = TAG_DONE;
  162.  
  163.     if (style & (FSF_BOLD|FSF_ITALIC)) {
  164.     /*
  165.      * The following code will determine if the typeface is an outline
  166.      * typeface.  If it is, it will try to find an alternate typeface
  167.      * that better matches the requested style.
  168.      *
  169.      * There are two comments I would like to make about how this is
  170.      * done.  First, it would probably be better to skip the .font
  171.      * file checking, even though this is how the diskfont library
  172.      * itself determines that an .otag file exists.  If the .otag
  173.      * file is looked for right away, it would make the following code
  174.      * faster and more flexible if future font contents identifiers
  175.      * are assigned.  Second, when an application looks for an
  176.      * alternate typeface it gets a better typeface, but it sacrifices
  177.      * installation independence -- i.e. the appearance of a stylistic
  178.      * variation depends on whether that variation is installed on a
  179.      * particular machine.  Admittadly, this variation is just a
  180.      * subtle form of the already existing font substitution problem.
  181.      */
  182.  
  183.     /*   build file path specification for face's font contents file */
  184.     if (strrchr(faceName, ':')) {
  185.         /* face name contains absolute path specification already */
  186.         strcpy(FilePath, faceName);
  187.     }
  188.     else {
  189.         /* face name is reletive to the default FONTS: path */
  190.         strcpy(FilePath, "FONTS:");
  191.         AddPart(FilePath, faceName, 256);
  192.     }
  193.  
  194.     /*   open font contents file */
  195.     fFile = Open(FilePath, MODE_OLDFILE);
  196.     if (fFile == 0) {
  197.         /* no font contents file: no font */
  198.         printf("no font contents file \"%s\"\n", FilePath);
  199.         return(0);
  200.     }
  201.     
  202.     actual = Read(fFile, &fch, sizeof(fch));
  203.     Close(fFile);
  204.  
  205.     if ((actual != sizeof(fch)) || ((fch.fch_FileID & 0xfff0) != FCH_ID)) {
  206.         /* failure to read valid font contents file header */
  207.         printf("fch failure: actual %ld, id $%04lx\n", actual,
  208.             fch.fch_FileID);
  209.         return(0);
  210.     }
  211.  
  212.     if (fch.fch_FileID == OFCH_ID) {
  213.         /* .font file indicates an .otag file exists */
  214.         /* generate .otag file path */
  215.         s = (char *) strrchr(FilePath, '.');
  216.         strcpy(s, OTSUFFIX);
  217.  
  218.         /* open .otag file */
  219.         if (!(fFile = Open(FilePath, MODE_OLDFILE))) {
  220.         printf("no outline tag file \"%s\"\n", FilePath);
  221.         return(0);
  222.         }
  223.  
  224.         /* read and verify the .otag */
  225.         actual = Read(fFile, &tag, sizeof(tag));
  226.         Seek(fFile, 0, OFFSET_END);
  227.         if ((actual == sizeof(tag)) && (tag.ti_Tag == OT_FileIdent) &&
  228.             (Seek(fFile, 0, OFFSET_BEGINNING) == tag.ti_Data) &&
  229.             (tags = (struct TagItem *) AllocVec(tag.ti_Data, 0))) {
  230.         /* this is a valid .otag file header */
  231.         if (Read(fFile, tags, tag.ti_Data) == tag.ti_Data) {
  232.             /* patch indirect pointers */
  233.             for (i = 0; i < tag.ti_Data/sizeof(struct TagItem); i++) {
  234.             if (tags[i].ti_Tag == TAG_DONE)
  235.                 break;
  236.             if (tags[i].ti_Tag & OT_Indirect)
  237.                 tags[i].ti_Data += (ULONG) tags;
  238.             }
  239.  
  240.             /* find alternate font for style */
  241.             s = 0;
  242.             x = GetTagData(OT_StemWeight, OTS_Medium, tags);
  243.             y = GetTagData(OT_SlantStyle, OTS_Upright, tags);
  244.             if ((style & (FSF_ITALIC|FSF_BOLD)) ==
  245.                 (FSF_ITALIC|FSF_BOLD)) {
  246.             /* check if bold italic is appropriate */
  247.             if ((x < ((OTS_Medium+OTS_SemiBold)/2)) &&
  248.                 (y == OTS_Upright))
  249.                 s = (char *) GetTagData(OT_BIName, 0, tags);
  250.             }
  251.             if ((!s) && (style & FSF_ITALIC)) {
  252.             /* ensure italic */
  253.             if (y == OTS_Upright)
  254.                 s = (char *) GetTagData(OT_IName, 0, tags);
  255.             }
  256.             if ((!s) && (style & FSF_BOLD)) {
  257.             /* ensure bold */
  258.             if (x < ((OTS_Medium+OTS_SemiBold)/2))
  259.                 s = (char *) GetTagData(OT_BName, 0, tags);
  260.             }
  261.             if (s) {
  262.             strcpy(FaceName, s);
  263.             strcat(FaceName, ".font");
  264.             }
  265.         }
  266.         FreeVec(tags);
  267.         }
  268.         Close(fFile);
  269.     }
  270.     }
  271.  
  272.     printf("ta %s style $%lx\n", tTextAttr.tta_Name,
  273.         tTextAttr.tta_Style);
  274.  
  275.     return(OpenDiskFont((struct TextAttr *) &tTextAttr));
  276. }
  277.  
  278.  
  279. void
  280. DisplaySample(tf, string)
  281. struct TextFont *tf;
  282. char *string;
  283. {
  284.     printf("tf %s style $%lx flags $%lx\n", tf->tf_Message.mn_Node.ln_Name,
  285.         tf->tf_Style, tf->tf_Flags);
  286.     SetFont(RastPort, tf);
  287.     Text(RastPort, string, strlen(string));
  288.     CloseFont(tf);
  289. }
  290.  
  291.  
  292. void
  293. DemoMain()
  294. {
  295.     struct TextFont *tf;
  296.  
  297.     /* GetAvailFonts(); */
  298.     Move(RastPort, 10,  50);
  299.     if (tf = OpenAspectedFont("CGTimes.font", 20, 50, FS_NORMAL))
  300.     DisplaySample(tf, "Times50:100 ");
  301.     if (tf = OpenAspectedFont("CGTimes.font", 20, 100, FS_NORMAL))
  302.     DisplaySample(tf, "Times100:100 ");
  303.     if (tf = OpenAspectedFont("CGTimes.font", 20, 200, FS_NORMAL))
  304.     DisplaySample(tf, "Times100:50 ");
  305.     Move(RastPort, 10, 80);
  306.     if (tf = OpenAspectedFont("CGTimes.font", 20, 100, FSF_BOLD))
  307.     DisplaySample(tf, "Times100:100b ");
  308.     if (tf = OpenAspectedFont("CGTimes.font", 20, 100, FSF_ITALIC))
  309.     DisplaySample(tf, "Times100:100i ");
  310.     if (tf = OpenAspectedFont("CGTimes.font", 20, 100, FSF_BOLD+FSF_ITALIC))
  311.     DisplaySample(tf, "Times100:100bi ");
  312.     Move(RastPort, 10, 110);
  313.     if (tf = OpenAspectedFont("topaz.font", 20, 100, FS_NORMAL))
  314.     DisplaySample(tf, "topaz100:100 ");
  315.     if (tf = OpenAspectedFont("topaz.font", 20, 100, FSF_ITALIC))
  316.     DisplaySample(tf, "topaz100:100i ");
  317.     if (tf = OpenAspectedFont("topaz.font", 20, 100, FSF_BOLD))
  318.     DisplaySample(tf, "topaz100:100b ");
  319.     Move(RastPort, 10, 140);
  320.     if (tf = OpenAspectedFont("topaz.font", 20, 50, FS_NORMAL))
  321.     DisplaySample(tf, "topaz50:100 ");
  322.     if (tf = OpenAspectedFont("topaz.font", 20, 200, FS_NORMAL))
  323.     DisplaySample(tf, "topaz100:50 ");
  324.     Move(RastPort, 10, 170);
  325.     if (tf = OpenAspectedFont("topaz.font", 8, 100, FS_NORMAL))
  326.     DisplaySample(tf, "topaz8-100:100 ");
  327.     if (tf = OpenAspectedFont("topaz.font", 8, 50, FS_NORMAL))
  328.     DisplaySample(tf, "topaz8-50:100 ");
  329.     if (tf = OpenAspectedFont("topaz.font", 8, 200, FS_NORMAL))
  330.     DisplaySample(tf, "topaz8-100:50 ");
  331.     Move(RastPort, 10, 200);
  332.     if (tf = OpenAspectedFont("topaz.font", 9, 100, FS_NORMAL))
  333.     DisplaySample(tf, "topaz9-100:100 ");
  334.     if (tf = OpenAspectedFont("topaz.font", 9, 50, FS_NORMAL))
  335.     DisplaySample(tf, "topaz9-50:100 ");
  336.     if (tf = OpenAspectedFont("topaz.font", 9, 200, FS_NORMAL))
  337.     DisplaySample(tf, "topaz9-100:50 ");
  338.     Move(RastPort, 10, 230);
  339.     if (tf = OpenAspectedFont("topaz.font", 10, 100, FS_NORMAL))
  340.     DisplaySample(tf, "topaz10-100:100 ");
  341.     if (tf = OpenAspectedFont("topaz.font", 10, 50, FS_NORMAL))
  342.     DisplaySample(tf, "topaz10-50:100 ");
  343.     if (tf = OpenAspectedFont("topaz.font", 10, 200, FS_NORMAL))
  344.     DisplaySample(tf, "topaz10-100:50 ");
  345.     Move(RastPort, 10, 260);
  346.     if (tf = OpenAspectedFont("topaz.font", 11, 100, FS_NORMAL))
  347.     DisplaySample(tf, "topaz11-100:100 ");
  348.     if (tf = OpenAspectedFont("topaz.font", 11, 50, FS_NORMAL))
  349.     DisplaySample(tf, "topaz11-50:100 ");
  350.     if (tf = OpenAspectedFont("topaz.font", 11, 200, FS_NORMAL))
  351.     DisplaySample(tf, "topaz11-100:50 ");
  352. }
  353.  
  354. main()
  355. {
  356.     struct IntuiMessage *im;
  357.     ULONG signal;
  358.  
  359.     _ONBREAK = (int (*)()) CXBRK;
  360.     /* validate library versions */
  361.     if (SysBase->lib_Version < 36)
  362.     EndGame(RETURN_ERROR, "exec.library version < 36\n");
  363.     if (DOSBase->lib_Version < 36)
  364.     EndGame(RETURN_ERROR, "dos.library version < 36\n");
  365.  
  366.     /* open libraries */
  367.     if (!(GfxBase = (struct Library *) OpenLibrary("graphics.library", 36)))
  368.     EndGame(RETURN_ERROR, "graphics.library version 36 open failure\n");
  369.     if (!(IntuitionBase = (struct Library *)
  370.         OpenLibrary("intuition.library", 36)))
  371.     EndGame(RETURN_ERROR, "intuition.library version 36 open failure\n");
  372.     if (!(DiskfontBase = (struct Library *)
  373.         OpenLibrary("diskfont.library", 36)))
  374.     EndGame(RETURN_ERROR, "diskfont.library version 36 open failure\n");
  375.     if (!(UtilityBase = (struct Library *)
  376.         OpenLibrary("utility.library", 36)))
  377.     EndGame(RETURN_ERROR, "utility.library version 36 open failure\n");
  378.  
  379.     /* get the output screen */
  380.     LockedScreen = Screen = LockPubScreen(0);
  381.  
  382.     /* open the window */
  383.     if (!(Window = OpenWindowTags(&NW,
  384.         WA_Height, Screen->Height,
  385.         WA_Title, "ScalableOutlineFonts Demo",
  386.         WA_AutoAdjust, TRUE,
  387.         WA_PubScreen, Screen,
  388.         TAG_DONE)))
  389.     EndGame(RETURN_ERROR, "OpenWindow failed\n");
  390.  
  391.     /* now that the window is open, the screen lock can be let go */
  392.     UnlockPubScreen(0, LockedScreen);
  393.     LockedScreen = 0;
  394.  
  395.     RastPort = Window->RPort;
  396.  
  397.     DemoMain();
  398.  
  399.     for (;;) {
  400.     while (im = (struct IntuiMessage *) GetMsg(Window->UserPort)) {
  401.         if (im->Class == CLOSEWINDOW) {
  402.         ReplyMsg((struct Message *) im);
  403.         EndGame(0);
  404.         }
  405.         ReplyMsg((struct Message *) im);
  406.     }
  407.     signal = Wait((1<<Window->UserPort->mp_SigBit)|SIGBREAKF_CTRL_C);
  408.     if (signal & SIGBREAKF_CTRL_C)
  409.         CXBRK();
  410.     }
  411. }
  412.